package cs3343.project.family.UI;

import java.awt.BasicStroke;
import java.awt.Color;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;

import cs3343.project.family.GameController;
import cs3343.project.family.StatisticsController;
 

/**
 * The Class GameStatFrame.
 */
public class GameStatFrame extends JFrame {

	/** The gc. */
	private GameController gc;
	
	/** The statistics controller. */
	private StatisticsController statisticsController;
	
    /*
     * Instantiates a new game stat frame.
     *
     * @param gc the gc
     * @param title the title
     * @param xAxis the x axis
     * @param yAxis the y axis
     */
    public GameStatFrame(GameController gc, String title, String xAxis, String yAxis) {		
        super(title);
		
        this.gc = gc;
		statisticsController = this.gc.getStatisticsController();
		
        CategoryDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset, title, xAxis, yAxis);
        ChartPanel chartPanel = new ChartPanel(chart);
        this.setSize(700, 400);
        
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        gc.getUiController().frameCentering(this);
        setContentPane(chartPanel);
    }

    /**
     * Creates the dataset.
     *
     * @return the category dataset
     */
    private CategoryDataset createDataset() {
    	
    	int totalTurn = gc.getStatisticsController().getPlayerEsteemHistory().get(0).size()/2;
    	
        String[] round = new String[totalTurn];
        for(int i = 0; i < totalTurn; i++)
        	round[i] = String.valueOf(i+1); 
            
        // create the dataset
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();


        //add esteem point of each round of each player into dataset
        for(int i = 0; i < gc.getStatisticsController().getPlayerEsteemHistory().size(); i++){
        	for(int j = 0; j < totalTurn; j++){
        		dataset.addValue(gc.getStatisticsController().getPlayerEsteemHistory().get(i).get(j)
        						, gc.getCharacterController().getPlayer(i).getName(), 
        						round[j]);
        	}
        }

        return dataset;
                
    }
    
    /**
     * Creates the chart.
     *
     * @param dataset the dataset
     * @param title the title
     * @param xAxis the x axis
     * @param yAxis the y axis
     * @return the j free chart
     */
    private JFreeChart createChart(final CategoryDataset dataset, String title, String xAxis, String yAxis) {
        
        // create the chart...
        final JFreeChart chart = ChartFactory.createLineChart(
            title,      			 // chart title
            xAxis,                    // domain axis label
            yAxis,      	             // range axis label
            dataset,                   // data
            PlotOrientation.VERTICAL,  // orientation
            true,                      // include legend
            true,                      // tooltips
            false                      // urls
        );

        chart.setBackgroundPaint(Color.white);

        final CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.white);

        // customise the range axis...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setAutoRangeIncludesZero(true);
        
        // customise the renderer...
        final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();

        renderer.setSeriesStroke(
            0, new BasicStroke(
                2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                1.0f, new float[] {10.0f, 6.0f}, 0.0f
            )
        );
        renderer.setSeriesStroke(
            1, new BasicStroke(
                2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                1.0f, new float[] {6.0f, 6.0f}, 0.0f
            )
        );
        renderer.setSeriesStroke(
            2, new BasicStroke(
                2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                1.0f, new float[] {2.0f, 6.0f}, 0.0f
            )
        );        
        return chart;
    }
}
